package com.regiee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regiee.common.R;
import com.regiee.dto.SetmealDto;
import com.regiee.entity.Category;
import com.regiee.entity.Dish;
import com.regiee.entity.Setmeal;
import com.regiee.entity.SetmealDish;
import com.regiee.service.CategoryService;
import com.regiee.service.SetmealDishService;
import com.regiee.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealDishController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /*
    分页查询套餐接口
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<SetmealDto> pageInfo=new Page<>();
        Page<Setmeal> pageMeal=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<Setmeal>()
                .like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        setmealService.page(pageMeal,wrapper);
        //将这个类查出来的拷贝到DTO中
        BeanUtils.copyProperties(pageMeal,pageInfo,"records");
        List<Setmeal> list=pageMeal.getRecords();
        List<SetmealDto> list1=new ArrayList<>();
        //然后再进行将Setmeal的数据操作出来
        for(Setmeal sm:list){
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(sm,setmealDto);
            Long Id=sm.getCategoryId();
            String categoryName=categoryService.getById(Id).getName();
            setmealDto.setCategoryName(categoryName);
            list1.add(setmealDto);
        }
        pageInfo.setRecords(list1);
        return R.success(pageInfo);
    }
    /*
    新增套餐接口
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }
    /*
    查询返回显示,根据Id查询
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        return R.success(setmealService.getSetmealById(id));
    }
    /*
    修改套餐接口
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateById(setmealDto);
        return R.success("修改成功");
    }
    /*
    批量停售,停售单个
     */
    @PostMapping("/status/0")
    public R<String> UpdateStatus0(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
            Setmeal st=setmealService.getById(id[0]);
            st.setStatus(0);
            setmealService.updateById(st);
            return R.success("修改成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<Setmeal>()
                .in(Setmeal::getId, list);
        List<Setmeal> list1=setmealService.list(wrapper);
        for(Setmeal sst:list1){
            sst.setStatus(0);
        }
        setmealService.updateBatchById(list1);
        return R.success("修改成功");
    }
    /*
    批量起售，起售单个
     */
    @PostMapping("/status/1")
    public R<String> UpdateStatus1(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
            Setmeal st=setmealService.getById(id[0]);
            st.setStatus(1);
            setmealService.updateById(st);
            return R.success("修改成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<Setmeal>()
                .in(Setmeal::getId, list);
        List<Setmeal> list1=setmealService.list(wrapper);
        for(Setmeal sst:list1){
            sst.setStatus(1);
        }
        setmealService.updateBatchById(list1);
        return R.success("修改成功");
    }
    /*
    删除接口
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        Long[] id=ids;
        if(id.length==1){
            setmealService.removeById(id[0]);
            return R.success("删除成功");
        }
        List<Long> list=new ArrayList<>();
        for(int i=0;i<id.length;i++){
            list.add(i,id[i]);
        }
        setmealService.removeBatchByIds(list);
        return R.success("删除成功");
    }
}
